package com.xms.form.api.controller;

import com.xms.core.exception.XmsException;
import com.xms.core.model.KeyValuePair;
import com.xms.form.api.model.CreateFormModel;
import com.xms.form.core.FormType;
import com.xms.form.entity.SystemForm;
import com.xms.form.service.ISystemFormCreaterService;
import com.xms.schema.entity.Entity;
import com.xms.schema.service.entity.IEntityFinderService;
import com.xms.utils.UUIDUtil;
import com.xms.web.framework.controller.ApiBaseController;
import com.xms.web.framework.model.JsonResultObject;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.sql.SQLException;
import java.time.LocalDateTime;

/**
 * <p>
 * SystemFormCreaterController
 * </p>
 *
 * @author migo
 * @since 2021/9/18
 */
@RestController
@RequestMapping("{org}/api/schema/form")
public class SystemFormCreaterController extends ApiBaseController {
    private ISystemFormCreaterService _systemFormCreaterService;
    private IEntityFinderService _entityFinderService;

    @Autowired
    public void SystemFormCreaterController(ISystemFormCreaterService systemFormCreaterService,
                                            IEntityFinderService entityFinderService) {
        _systemFormCreaterService = systemFormCreaterService;
        _entityFinderService = entityFinderService;
    }

    @ApiOperation("新建表单")
    @PostMapping("create")
    public JsonResultObject create(@RequestBody CreateFormModel model) throws XmsException, SQLException {
        if (UUIDUtil.isEmpty(model.entityid)) {
            return JNotSpecifiedParams("entityid");
        }
        Entity entity = _entityFinderService.getById(model.entityid);
        if (entity == null) {
            return JNotFound("entityid=" + model.entityid);
        }
        SystemForm form = new SystemForm();
        form.setCreatedby(_currentUser.getUser().SystemUserId);
        form.setCreatedon(LocalDateTime.now());
        form.setEntityid(model.entityid);
        form.setEntityname(entity.getName());
        form.setFormtype(FormType.Main);
        form.setAuthorizationenabled(false);
        form.setCanbedeleted(true);
        form.setIscustombutton(model.iscustombutton);
        form.setCustombuttons(model.custombuttons);
        form.setDescription(model.description);
        form.setFormconfig(model.formconfig);
        form.setIscustomizable(true);
        form.setIsdefault(false);
        form.setName(model.name);
        form.setOrganizationid(_currentUser.getUser().OrganizationId);
        form.setStatecode(true);
        form.setSystemformid(UUIDUtil.get());
        if (_systemFormCreaterService.create(form)) {
            return JCreateSuccess(KeyValuePair.build("id", form.getSystemformid()));
        }
        return JCreateFailure();
    }
}
